﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CasinoApp.Views
{
    /// <summary>
    /// UI-Control für die Anzeige des Inhalts eines SlotMachine-Slots.
    /// </summary>
    public partial class SlotMachineBarControl : UserControl
    {
        #region Fields

        /// <summary>
        /// Depenency-Property für das Setzen des Top-Margin-Wert des ersten Symbol-Streifen.
        /// </summary>
        public static readonly DependencyProperty Stripe1TopMarginProperty =
           DependencyProperty.Register("Stripe1TopMarginProperty", typeof(double),
           typeof(SlotMachineBarControl), new PropertyMetadata(Stripe1TopMarginPropertyChanged)
        );

        /// <summary>
        /// Depenency-Property für das Setzen des Top-Margin-Wert des zweiten Symbol-Streifen.
        /// </summary>
        public static readonly DependencyProperty Stripe2TopMarginProperty =
         DependencyProperty.Register("Stripe2TopMarginProperty", typeof(double), 
         typeof(SlotMachineBarControl), new PropertyMetadata(Stripe2TopMarginPropertyChanged)
        );

        #endregion // Fields

        #region Konstruktoren

        /// <summary>
        /// Standard-Konstruktor.
        /// </summary>
        public SlotMachineBarControl()
        {
            InitializeComponent();
        }

        #endregion // Konstruktoren

        #region Properties

        /// <summary>
        /// Gibt den Top-Margin-Wert des ersten Symbol-Streifens an oder ruft ihn ab.
        /// </summary>
        public double Stripe1TopMargin
        {
            get { return (double)GetValue(Stripe1TopMarginProperty); }
            set { SetValue(Stripe1TopMarginProperty, value); }
        }

        /// <summary>
        /// Gibt den Top-Margin-Wert des zweiten Symbol-Streifens an oder ruft ihn ab.
        /// </summary>
        public double Stripe2TopMargin
        {
            get { return (double)GetValue(Stripe2TopMarginProperty); }
            set { SetValue(Stripe2TopMarginProperty, value); }
        }

        #endregion // Properties

        #region Methoden

        /// <summary>
        /// Führt die Spinning-Animation aus.
        /// </summary>
        public void StartSpinning()
        {
            VisualStateManager.GoToState(this, "SpinningOn", false);
        }

        /// <summary>
        /// Stoppt die Spinning-Animation und zeigt das entsprechende Symbol an.
        /// </summary>
        /// <param name="winningSymbol"></param>
        public void StopAtSymbol(int winningSymbol)
        {
            Dispatcher.BeginInvoke(delegate 
            {
                SetResultImage(winningSymbol);

                VisualStateManager.GoToState(this, "SpinningOff", false);
            }); 
        }

        /// <summary>
        /// Setzt anhand der Symbol-Nr das entsprechende Bild auf das Resultats-Image-Control.
        /// </summary>
        /// <param name="symbolNr"></param>
        private void SetResultImage(int symbolNr)
        {
            System.Windows.Media.Imaging.BitmapImage imgSource = new System.Windows.Media.Imaging.BitmapImage();
            string imagePath = (string.Format("{0}SymbolView_0{1}.png", "../_Images/SlotMachine/", symbolNr));
            imgSource.UriSource = new Uri(imagePath, UriKind.Relative);

            resultImage.Source = imgSource;
        }

        #endregion // Methoden

        #region Event-Handler

        /// <summary>
        /// Wird aufgerufen wenn der Top-Margin-Wert des ersten Symbol-Streifens verändert werden soll.
        /// </summary>
        /// <param name="d">DependencyObject SlotMachineBarControl.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void Stripe1TopMarginPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SlotMachineBarControl target = (SlotMachineBarControl)d;
            target.stripeImage1.Margin = new Thickness(0, (double)e.NewValue, 0, 0);
        }

        /// <summary>
        /// Wird aufgerufen wenn der Top-Margin-Wert des ersten Symbol-Streifens verändert werden soll.
        /// </summary>
        /// <param name="d">DependencyObject SlotMachineBarControl.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void Stripe2TopMarginPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SlotMachineBarControl target = (SlotMachineBarControl)d;
            target.stripeImage2.Margin = new Thickness(0, (double)e.NewValue, 0, 0);
        }

        #endregion // Event-Handler
    }
}
